package com.winter.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.IenterpriseFinancingMapper;
import com.winter.mapper.StandardAssetsMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.AssetPlatform;
import com.winter.model.DictEntry;
import com.winter.model.IenterpriseFinancing;
import com.winter.model.StandardAssets;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
@Transactional
public class StandardAssetsService {

    @Resource
    private StandardAssetsMapper standardAssetsMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private DictEntryMapper dictEntryMapper;
    @Resource
    private IenterpriseFinancingMapper ienterpriseFinancingMapper;

    /**
     * 筛选非标以及资金平台列表信息
     *
     * @return list
     */
    public Result getSandardAssetsList(Integer pageNumber, Integer pageSize, String dcFinancingType, String assureFlg, String assureType, String userNo,String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        /*if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }*/
        List<StandardAssets> stList = standardAssetsMapper.getSandardAssetsList(dcFinancingType, assureFlg, assureType, userNo, key);
        PageInfo<StandardAssets> pageInfo = new PageInfo<>(stList);
        return Result.success(pageInfo);
    }

    /**
     * 根据Id查询 发布的资金平台信息
     */
    @SuppressWarnings("unused")
    public Result querySandardId(Long id,String userNo) {
    	UserInfo uit=userInfoMapper.selectByUserNo(userNo);
    	if(null!=uit){
    		if(uit.getAuditFlg().equals("0")){
    			return Result.failure(ResultCode.PARAM_IS_BLANK, "该会员未认证,无详情查看权限");
    		}
    	}
        StandardAssets ient = standardAssetsMapper.selectByPrimaryKey(id);
        if (null != ient) {
        	if(uit.getIsvip().equals("1")){
            if (null != ient.getCreateUser()) {
                UserInfo ui = userInfoMapper.selectByUserNo(ient.getCreateUser());
                if (null != ui) {
                    if (null != ui.getCustTel()) {
                        ient.setUserTel(ui.getCustTel());
                    }
                    if (null != ui.getCustMobile()) {
                        ient.setUserMobile(ui.getCustMobile());
                    }
                    if (null != ui.getCustEmail()) {
                        ient.setUserEmail(ui.getCustEmail());
                    }
                }
            }
        }
            DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ient.getFinAmountUnit());
            if (null != ty) {
        		/*if(ty.getDictname().equals("万")){
        			ient.setFinAmountUnit("万");
        		}else if(ty.getDictname().equals("亿")){
        			ient.setFinAmountUnit("亿");
        		}*/
                ient.setFinAmountUnit(ty.getDictname());
            }

            DictEntry ty2 = dictEntryMapper.get("DC_FIN_DATE_UNIT", ient.getFinDeadlineUnit());
            if (null != ty2) {
        		/*if(ty2.getDictname().equals("月")){
        			ient.setFinDeadlineUnit("月");
        		}else if(ty2.getDictname().equals("年")){
        			ient.setFinDeadlineUnit("年");
        		}else if(ty2.getDictname().equals("日")){
        			ient.setFinDeadlineUnit("日");
        		}*/
                ient.setFinDeadlineUnit(ty2.getDictname());
            }

            if (null != ient.getStateOwnFlg()) {
                if (ient.getStateOwnFlg()
                        .equals("1")) {
                    ient.setStateOwnFlg("是");
                } else if (ient.getStateOwnFlg()
                               .equals("0")) {
                    ient.setStateOwnFlg("否");
                }
            }
            if (null != ient.getAssureFlg()) {
                if (ient.getAssureFlg()
                        .equals("1")) {
                    ient.setAssureFlg("是");
                } else if (ient.getAssureFlg()
                               .equals("0")) {
                    ient.setAssureFlg("无");
                }
            }

            DictEntry ty3 = dictEntryMapper.get("DC_FINANCING_TYPE", ient.getDcFinancingType());
            DictEntry ty4 = dictEntryMapper.get("DC_FINANCING_TYPE_CURRENCY", ient.getSubclass());
            if (null != ty3) {
                ient.setDcFinancingType(ty3.getDictname());
            } else if (null != ty4) {
                ient.setDcFinancingType(ty4.getDictname());
            } else if (null != ty3 && null != ty4) {
                ient.setDcFinancingType(ty3.getDictname() + "-" + ty4.getDictname());
            }

            // 担保方式
            DictEntry ty5 = dictEntryMapper.get("GUARANTEE_MODE", ient.getAssureType());
            if (ty5 != null) {
                ient.setAssureType(ty5.getDictname());
            }

        }
        return Result.success(ient);
    }

    /**
     * 新增非标资金平台信息
     *
     * @return int
     */
    public Result insertStand(StandardAssets ien) {
        if (null != ien) {
            if (Strings.isNullOrEmpty(ien.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            UserInfo uit=userInfoMapper.selectByUserNo(ien.getCreateUser());
        	if(null!=uit){
        		if(uit.getAuditFlg().equals("0")){
        			return Result.failure(ResultCode.PARAM_IS_BLANK, "该会员未认证,无详情查看权限");
        		}
        	}
            if (Strings.isNullOrEmpty(ien.getDcFinancingType())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品类型");
            }
            if (ien.getAssureFlg()
                   .equals("0")) {
                ien.setAssureType("0");
            } else {
                if (Strings.isNullOrEmpty(ien.getAssureType())) {
                    return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择担保方式");
                }
            }

            if (Strings.isNullOrEmpty(ien.getSubclass())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品子项类型");
            }
            if (Strings.isNullOrEmpty(ien.getProName())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入产品名称");
            }
            if (Strings.isNullOrEmpty(ien.getFinOwner())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资主体");
            }
            if (Strings.isNullOrEmpty(ien.getFinUsefor())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资用途");
            }
            if (0 >= ien.getFinAmoutn()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资金额");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ien.getFinAmountUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("万")) {
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    } else if (ty.getDictname()
                                 .equals("亿")) {
                        ien.setFinAmoutn(ien.getFinAmoutn() * 10000);
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    }
                }
            }
            if (0 >= ien.getFinDeadline()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资期限");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_DATE_UNIT", ien.getFinDeadlineUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("月")) {
                        //ien.setFinDeadline(ien.getFinDeadline());
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("年")) {
                        ien.setFinDeadline(ien.getFinDeadline() * 12);
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("日")) {
                        //ien.setFinDeadline(ien.getFinDeadline());
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    }
                }
            }
            if (ien.getExpectAnnualYearLow() == null || ien.getExpectAnnualYearHigh() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入利率");
            } else {
                if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow() + "-" + ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() == "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearHigh());
                    ien.setExpectAnnualYearLow(ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() == "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow());
                    ien.setExpectAnnualYearHigh(ien.getExpectAnnualYearLow());
                }

            }

            ien.setUpdateUser(ien.getCreateUser());
            ien.setCreatetime(new Date());
            ien.setUpdatetime(new Date());
            ien.setIsvalid("1");
            ien.setAuditing("0");//0.审核中，1完成，2.失败
            ien.setProStatus("1");
            ien.setProType("2");//1.债券；2.非标。
            int i = standardAssetsMapper.insertSelective(ien);
            if (i > 0) {
                return Result.success("添加成功");
            } else {
                return Result.failure(ResultCode.INSERT_ERROR);
            }
        } else {
            return Result.success("添加参数为空");
        }
    }

    /**
     * 我的资产信息列表
     */
    public Result getIenterStandList(Integer pageNumber, Integer pageSize, String userNo) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        List<StandardAssets> stList = standardAssetsMapper.getIenterStandList(userNo);
        PageInfo<StandardAssets> pageInfo = new PageInfo<>(stList);
        return Result.success(pageInfo);
    }

    /**
     * 根据产品id和资产分类修改产品状态
     */
    public Result getIenterStandId(Long id, String proType, String userNo, String status) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (id <= 0) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "id不能为0或者空");
        }
        if (Strings.isNullOrEmpty(status)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "产品操作类型不能为空");
        }
        if (Strings.isNullOrEmpty(proType)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "产品分类不能为空");
        } else {
            if (proType.equals("1")) {
                if (status.equals("1")) {
                    ienterpriseFinancingMapper.deleteByPrimaryKey(Short.valueOf(id + ""));
                    return Result.success("删除我的债券产品成功");

                } else if (status.equals("2")) {
                    IenterpriseFinancing ien = ienterpriseFinancingMapper.selectByPrimaryKey(Long.valueOf(id + ""));
                    if (null != ien) {
                        ien.setProStatus("2");
                        ienterpriseFinancingMapper.updateByPrimaryKeySelective(ien);
                        return Result.success("债券状态修改成功");
                    }
                } else if (status.equals("3")) {
                    IenterpriseFinancing ient = ienterpriseFinancingMapper.selectByPrimaryKey(Long.valueOf(id + ""));
                    DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ient.getFinAmountUnit());
                    if (null != ty) {
                        ient.setFinAmountUnit(ty.getDictname());
                    }

                    DictEntry ty2 = dictEntryMapper.get("DC_FIN_DATE_UNIT", ient.getFinDeadlineUnit());
                    if (null != ty2) {
                        ient.setFinDeadlineUnit(ty2.getDictname());
                    }

                    if (null != ient.getStateOwnFlg()) {
                        if (ient.getStateOwnFlg()
                                .equals("1")) {
                            ient.setStateOwnFlg("是");
                        } else if (ient.getStateOwnFlg()
                                       .equals("0")) {
                            ient.setStateOwnFlg("否");
                        }
                    }
                    if (null != ient.getAssureFlg()) {
                        if (ient.getAssureFlg()
                                .equals("1")) {
                            ient.setAssureFlg("是");
                        } else if (ient.getAssureFlg()
                                       .equals("0")) {
                            ient.setAssureFlg("无");
                        }
                    }

                    DictEntry ty3 = dictEntryMapper.get("DC_FINANCING_TYPE", ient.getDcFinancingType());
                    DictEntry ty4 = dictEntryMapper.get("DC_FINANCING_TYPE_CURRENCY", ient.getSubclass());
                    ient.setDcFinancingType(ty3.getDictname() + "-" + ty4.getDictname());
                    return Result.success(ient);
                }

            } else if (proType.equals("2")) {

                if (status.equals("1")) {
                    standardAssetsMapper.deleteByPrimaryKey(id);
                    return Result.success("删除我的非标产品成功");

                } else if (status.equals("2")) {
                    StandardAssets ien = standardAssetsMapper.selectByPrimaryKey(id);
                    if (null != ien) {
                        ien.setProStatus("2");
                        standardAssetsMapper.updateByPrimaryKeySelective(ien);
                        return Result.success("非标状态修改成功");
                    }
                } else if (status.equals("3")) {
                    StandardAssets ient = standardAssetsMapper.selectByPrimaryKey(id);
                    DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ient.getFinAmountUnit());
                    if (null != ty) {
                        ient.setFinAmountUnit(ty.getDictname());
                    }

                    DictEntry ty2 = dictEntryMapper.get("DC_FIN_DATE_UNIT", ient.getFinDeadlineUnit());
                    if (null != ty2) {
                        ient.setFinDeadlineUnit(ty2.getDictname());
                    }

                    if (null != ient.getStateOwnFlg()) {
                        if (ient.getStateOwnFlg()
                                .equals("1")) {
                            ient.setStateOwnFlg("是");
                        } else if (ient.getStateOwnFlg()
                                       .equals("0")) {
                            ient.setStateOwnFlg("否");
                        }
                    }
                    if (null != ient.getAssureFlg()) {
                        if (ient.getAssureFlg()
                                .equals("1")) {
                            ient.setAssureFlg("是");
                        } else if (ient.getAssureFlg()
                                       .equals("0")) {
                            ient.setAssureFlg("无");
                        }
                    }

                    DictEntry ty3 = dictEntryMapper.get("DC_FINANCING_TYPE", ient.getDcFinancingType());
                    DictEntry ty4 = dictEntryMapper.get("DC_FINANCING_TYPE_CURRENCY", ient.getSubclass());
                    ient.setDcFinancingType(ty3.getDictname() + "-" + ty4.getDictname());
                    return Result.success(ient);
                }

            }
        }
        return null;
    }

    /**
     * 更新非标资金平台信息
     *
     * @return int
     */
    public Result updateStand(StandardAssets ien) {
        if (null != ien) {
            if (Strings.isNullOrEmpty(ien.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            if (ien.getId() <= 0) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "产品id不能为空");
            }
            if (Strings.isNullOrEmpty(ien.getDcFinancingType())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品类型");
            }
            if (ien.getAssureFlg()
                   .equals("0")) {
                ien.setAssureType("0");
            } else {
                if (Strings.isNullOrEmpty(ien.getAssureType())) {
                    return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择担保方式");
                }
            }

            if (Strings.isNullOrEmpty(ien.getSubclass())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品子项类型");
            }
            if (Strings.isNullOrEmpty(ien.getProName())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入产品名称");
            }
            if (Strings.isNullOrEmpty(ien.getFinOwner())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资主体");
            }
            if (Strings.isNullOrEmpty(ien.getFinUsefor())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资用途");
            }
            if (0 >= ien.getFinAmoutn()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资金额");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ien.getFinAmountUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("万")) {
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    } else if (ty.getDictname()
                                 .equals("亿")) {
                        ien.setFinAmoutn(ien.getFinAmoutn() * 10000);
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    }
                }
            }
            if (0 >= ien.getFinDeadline()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资期限");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_DATE_UNIT", ien.getFinDeadlineUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("月")) {
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("年")) {
                        ien.setFinDeadline(ien.getFinDeadline() * 12);
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("日")) {
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    }
                }
            }
            if (ien.getExpectAnnualYearLow() == null || ien.getExpectAnnualYearHigh() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入利率");
            } else {
                if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow() + "-" + ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() == "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearHigh());
                    ien.setExpectAnnualYearLow(ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() == "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow());
                    ien.setExpectAnnualYearHigh(ien.getExpectAnnualYearLow());
                }

            }

            ien.setUpdateUser(ien.getCreateUser());
            //ien.setCreatetime(new Date());
            ien.setUpdatetime(new Date());
            //ien.setIsvalid("1");
            //ien.setAuditing("0");//0.审核中，1完成，2.失败
            //ien.setProStatus("1");
            //ien.setProType("2");//1.债券；2.非标。
            int i = standardAssetsMapper.updateByPrimaryKeySelective(ien);
            if (i > 0) {
                return Result.success("更新成功");
            } else {
                return Result.failure(ResultCode.INSERT_ERROR);
            }
        } else {
            return Result.success("更新参数为空");
        }
    }
    
    /**
     * 首页资产平台列表
     * @param userNo
     * @return
     */
    public Result assetsList(){
    	AssetPlatform platform = new AssetPlatform();
    	List<StandardAssets> assetsList = standardAssetsMapper.selectByIdKey();
    	if (assetsList.size() > 0) {
    		platform.setAssetsList(assetsList);
		}
    	
    	List<IenterpriseFinancing> financingList = ienterpriseFinancingMapper.selectByIdKey();
    	if (financingList.size() > 0) {
			platform.setFinancingList(financingList);
		}
    	return Result.success(platform);
    }

}
